/**
 * 工具类
 * @module tools
 */
import { Toast } from "mand-mobile"
import { wxSign } from '../api/wechat'
import wx from "weixin-js-sdk"
import config from "../config"
import store from "../tools/factory.js"

export default {
    getUrlKey: function () {
        let url = decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串   
        let theRequest = new Object();
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            let strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    },
    toChinaNum: function (num) {
        let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
        let unit = ["", "十", "百", "千", "万"];
        num = parseInt(num);
        let getWan = (temp) => {
            let strArr = temp.toString().split("").reverse();
            let newNum = "";
            for (var i = 0; i < strArr.length; i++) {
                newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
            }
            return newNum;
        }
        let overWan = Math.floor(num / 10000);
        let noWan = num % 10000;
        if (noWan.toString().length < 4) noWan = "0" + noWan;
        return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
    },
    // 判断ios还是android
    getDeviceType() {
        if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
            //Ios
            return "ios"
        } else if (/(Android)/i.test(navigator.userAgent)) {
            //Android终端
            return "android"
        }
    },
    //判断是否是微信浏览器的函数
    isWeiXin() {
        //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
        var ua = window.navigator.userAgent.toLowerCase();
        //通过正则表达式匹配ua中是否含有MicroMessenger字符串
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            return true;
        } else {
            return false;
        }
    },
    delQueStr(url, ref) {
        var str = "";
        if (url.indexOf('?') != -1) {
            str = url.substr(url.indexOf('?') + 1);
        }
        else {
            return url;
        }
        var arr = "";
        var returnurl = "";
        var setparam = "";
        if (str.indexOf('&') != -1) {
            arr = str.split('&');
            var i = 0;
            for (i in arr) {
                if (arr[i].split('=')[0] != ref) {
                    returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
                }
            }
            return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
        }
        else {
            arr = str.split('=');
            if (arr[0] == ref) {
                return url.substr(0, url.indexOf('?'));
            }
            else {
                return url;
            }
        }
    },
    // 微信签名
    wxShare(shareObj, noNeed) {
        wxSign({ url: location.href.split("#")[0] }).then(res => {
            let req = res.data;
            console.log("微信签名", req.data);
            if (req.success) {
                wx.config({
                    appId: req.data.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
                    timestamp: req.data.timestamp, // 必填，生成签名的时间戳
                    nonceStr: req.data.noncestr, // 必填，生成签名的随机串
                    signature: req.data.signature, // 必填，签名，见附录1
                    jsApiList: [
                        "updateAppMessageShareData",
                        "updateTimelineShareData",
                        "getLocation"
                    ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                });
                wx.ready(function () {
                    wx.checkJsApi({
                        jsApiList: [
                            "updateAppMessageShareData",
                            "updateTimelineShareData",
                            "getLocation"
                        ], // 需要检测的JS接口列表，所有JS接口列表见附录2,
                        success: function (res) {
                            if (!noNeed) {
                                wx.getLocation({
                                    type: "gcj02", // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                                    success: function (res) {
                                        store.set("lat", res.latitude);
                                        store.set("lng", res.longitude)
                                    },
                                    cancel: function (res) {
                                        console.log("用户拒绝授权获取地理位置");
                                    }
                                });
                            }
                        }
                    });
                    wx.updateAppMessageShareData(shareObj);
                    wx.updateTimelineShareData(shareObj);
                });
                wx.error(function (res) {
                    Toast.failed(res)
                });
            } else {
                Toast.failed(req.msg)
            }
        })
    }
}